package com.liy.redisson.service.impl;

import com.liy.redisson.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author ly
 * @date 2022/11/10 10:34
 */

@Service
public class SenMailServiceImpl implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    //发送人和配置文件中一致
    private String from = "3129943066@qq.com";
    //接收人
    private String to = "1027507347@qq.com";
    //标题
    private String  subject = "周俊伟吃中午放了";
    //正文
//    private String context = "测试邮件正文内容: 我想给你一件三辆";
    private String context = "请你快速查收并回复,24小时内有效";

    @Override
    public void sendMail() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(from);
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(context);
        javaMailSender.send(mailMessage);
    }

    @Override
    public void sendMailFuz() throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        //设置正文支持html解析
        helper.setText(context,true);
        javaMailSender.send(message);
    }

    @Override
    public void sendMailFuj() throws MessagingException {

        //发送人和配置文件中一致
        String from = "3129943066@qq.com";
        //接收人
        String to = "2240155377@qq.com";
        //标题
        String  subject = "周俊伟吃中饭了";
        //正文
        String context = "请你快速查看<a href='https://console-docs.apipost.cn/preview/e192de70ebd7c5c8/6a96fb601884ea33'>点击跳转</a>";

        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(context,true);

        //添加附件
        File file = new File("C:\\Users\\huangjinkui\\Desktop\\马甲平的崽.txt");
        File file2 = new File("C:\\Users\\huangjinkui\\Desktop\\18b54027edafeb54221676fce6155313.mp4");
        helper.addAttachment("证明",file);
        helper.addAttachment("视频",file2);

        javaMailSender.send(message);
    }
}
